package com.wcgksoft.controller.saleMan;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.common.util.MsgInfo;
import com.wcgksoft.entity.admin.Account;
import com.wcgksoft.entity.admin.Authority;
import com.wcgksoft.entity.goods.AddService;
import com.wcgksoft.entity.goods.AddServiceDetails;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.admin.AccountService;
import com.wcgksoft.service.goods.AddServiceService;
import com.wcgksoft.service.system.UserService;
import javafx.scene.chart.ValueAxis;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * @author 御风
 * @version V1.0
 * @Package com.wcgksoft.controller.saleMan
 * @Description TODO
 * @date 2020/5/17 14:44
 * @ClassName SaleManController
 */
@Controller
@RequestMapping("saleMan/sys")
public class SaleManController {

  @Autowired
  private UserService userService;

  @Autowired
  private AccountService accountService;
  @Autowired
  private AddServiceService addServiceService;

  @ResponseBody
  @RequestMapping(value = "findAll")
  public Layui findAll(@RequestParam(value = "page", required = false) Integer page,
                       @RequestParam(value = "limit", required = false) Integer limit,
                       @RequestParam(value = "keyWord", required = false) String keyWord) {
    DetachedCriteria criteria = DetachedCriteria.forClass(SysUser.class);
    Account account = (Account) SecurityUtils.getSubject().getSession().getAttribute("om_user");
    if (StringUtils.isNotEmpty(keyWord)) {
      criteria.add(Restrictions.or(Restrictions.like("name", "%" + keyWord + "%", MatchMode.ANYWHERE),
              Restrictions.like("phone", "%" + keyWord + "%", MatchMode.ANYWHERE),
              Restrictions.like("address", "%" + keyWord + "%", MatchMode.ANYWHERE)));
    }
    criteria.add(Restrictions.eq("status",1));
    criteria.add(Restrictions.eq("saleMan_id", account.getId()));
    criteria.addOrder(Order.desc("createDate"));
    List<SysUser> list = userService.findByConditions(criteria, page, limit);
    return Layui.data(0, "success", userService.getTotalCount(), list);
  }

  //****开设账号*****
  @ResponseBody
  @RequestMapping(value = "saveUser")
  public Layui saveUser(@RequestParam(value = "data", required = false) String data) {
    JSONObject param = JSONObject.fromObject(data);
    SysUser sysUser = null;
    if (null == param) {
      return Layui.data(0, "fail", 0, null);
    } else {
      sysUser = (SysUser) JSONObject.toBean(JSONObject.fromObject(data), SysUser.class);
      Account account = (Account) SecurityUtils.getSubject().getSession().getAttribute("om_user");
      sysUser.setAgent_id(account.getAgent_id());
      sysUser.setSaleMan_id(account.getId());
      sysUser.setStatus(1);
      sysUser.setHealthScore("0");
      sysUser.setCreateDate(new Date());
      userService.save(sysUser);
      Account account1 = accountService.findById(account.getAgent_id());
      Integer userNumber = account.getUserNumber();
      System.out.println(userNumber);
      account1.setUserNumber(userNumber + 1);
      accountService.saveMerge(account1);
      Integer number = account.getUserNumber();
      account.setUserNumber(number + 1);
      accountService.saveMerge(account);
      return Layui.data(0, "success", 1, Layui.addList(sysUser));
    }
  }

  //*****admin修改用户账号信息*******
  @ResponseBody
  @RequestMapping(value = "updateUser")
  public Layui saveUserInfo(@RequestParam(value = "data", required = false) String data) {
    JSONObject param = JSONObject.fromObject(data);
    SysUser sysUser = null;
    if (null == param) {
      return Layui.data(0, "fail", 0, null);
    } else {
      sysUser = (SysUser) JSONObject.toBean(JSONObject.fromObject(data), SysUser.class);
      sysUser.setStatus(1);
//            sysUser.setEndTime(null);
      userService.saveMerge(sysUser);
      return Layui.data(0, "success", 1, Layui.addList(sysUser));
    }

  }

  @ResponseBody
  @RequestMapping(value = "delete")
  public Layui delete(@RequestParam(value = "id", required = false) Integer id) {
    try {
      SysUser sysUser = userService.findById(id);
      userService.delete(sysUser);
      return Layui.data(0, "操作成功", 0, null);
    } catch (Exception e) {
      e.printStackTrace();
      return Layui.data(0, "删除失败", 0, null);
    }
  }


  @ResponseBody
  @RequestMapping(value = "findByAgent")
  public MsgInfo findByAgent(@RequestParam(value = "aid", required = false) Integer id) {
    MsgInfo msgInfo = new MsgInfo();
    Account account = accountService.findById(id);
    msgInfo.setData(account);
    return msgInfo;
  }

  @ResponseBody
  @RequestMapping(value = "findBySaleMan")
  public MsgInfo findBySaleMan(@RequestParam(value = "sid", required = false) Integer id) {
    MsgInfo msgInfo = new MsgInfo();
    Account account = accountService.findById(id);
    msgInfo.setData(account);
    return msgInfo;
  }

  /*
   * 保存增值服务
   * */
  @ResponseBody
  @RequestMapping(value = "saveaddservice")
  public Layui saveaddservice(@RequestParam(value = "id", required = false) Integer id,
                                @RequestParam(value = "addservice", required = false) Integer[] addservice) {
    SysUser service = userService.findById(id);
    HashSet<AddServiceDetails> serviceDetails = new HashSet<>();
    for (int i = 0; i < addservice.length; i++) {
      AddService service1 = addServiceService.findById(addservice[i]);
      AddServiceDetails details = new AddServiceDetails();
      details.setAddService(service1);
      details.setSysUser(service);
      serviceDetails.add(details);
    }
    service.setAddServiceDetails(serviceDetails);
    userService.saveMerge(service);
    return Layui.data(0, "操作成功", 0, null);
  }
}
